﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
	// Start is called before the first frame update
	public struct Hole
	{ //1、描述洞口的结构体
		public bool isAppear; //1、表示这个洞口是否出现了地鼠
		public int holeX; //1、表示洞口的 x 坐标
		public int holeY; //1、表示洞口的 y 坐标
		public GameObject mole; //2、该洞口出现的地鼠
	}

	public Hole[] holes; //1、保存所有洞口的信息
	private float intervalPosX = 2, intervalPosY = 1; //1、每个洞口的横向和纵向间隔
	public GameObject holeObj; //1、要实例化的洞口预制体
	public GameObject moleObj; //2、要实例化的地鼠预制体

	public Timer timer; //4、保存计时器

	public float appearFrequency = 0.5f;
	private bool canIncreaseMole = true; //7、判断是否修改地鼠出现频率

	// Start is called before the first frame update
	void Start()
	{
		InitMap(); //1、初始化洞口
		MoleAppearFrequency(appearFrequency); //7、控制地鼠出现频率
		// InvokeRepeating("MoleAppear", 0f, 0.5f); //2、测试地鼠出现的频率

		timer.CountDown(true); //4、开始计时
	}

	private void MoleAppearFrequency(float appearFrequency)
	{
		//2、控制地鼠的出现频率 7、加入修改频率的参数
		CancelInvoke(); //7、停止产生地鼠
		if (appearFrequency <= 0)
			appearFrequency = 0.1f;
		InvokeRepeating("MoleAppear", 0, appearFrequency); //7、重新开始产生地鼠
	}

	void Update()
    {
		CleanHoleState(); //2、检测并清除 isAppear

		if (timer.time < 15 && canIncreaseMole == true)
		{
			//7.修改地鼠出现的频率 
			appearFrequency -= 0.5f; //7、判断是否修改地鼠出现频率
			MoleAppearFrequency(appearFrequency);
			canIncreaseMole = false;
		}
		if (timer.time < 0)
		{
			//6、判断是否倒计时结束
			GameOver(); //6、游戏结束提示
		}
	}

	private void GameOver() //6、游戏结束的操作
	{
		timer.CountDown(false); //6、时间归零
		CancelInvoke(); //6、停止生成地鼠
	}

	//1、初始化场景洞口
	private void InitMap()
	{
		Vector2 originalPos = new Vector2(-2, -2); //1、洞口的初始坐标
		holes = new Hole[9]; //1、分配存储洞口信息内存
							 //1、初始化每一个洞口的信息并创建洞口对象
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				holes[i * 3 + j] = new Hole();
				holes[i * 3 + j].holeX = (int)(originalPos.x + j * intervalPosX); //1、计算每一个洞口的 x 坐标
				holes[i * 3 + j].holeY = (int)(originalPos.y + i * intervalPosY); //1、计算每一个洞口的 y 坐标
				holes[i * 3 + j].isAppear = false; //1、表示当前洞口没有地鼠
				Instantiate(holeObj,
					new Vector3(holes[i * 3 + j].holeX, holes[i * 3 + j].holeY, 0),
					Quaternion.identity); //1、实例化洞口对象
			}
		}
	}

	private void MoleAppear()
	{
		// 如果9个位置都被占满了，则不再生成了
		bool bBreakFunc = true;
		for(int i=0; i<9; i++)
        {
			if(!holes[i].isAppear)
            {
				bBreakFunc = false;
				break;
			}
		}

        if (bBreakFunc)
        {
			return;
        }

		//2、地鼠的出现控制
		int id = UnityEngine.Random.Range(0, 9); //2、获得随机数

		while (holes[id].isAppear == true)
		{
			//2、判断当前洞口是否已经有地鼠了，为了防止死机，需要再 Mole 类里边写上 Destroy 函数。
			id = UnityEngine.Random.Range(0, 9); //2、如果有，则重新获得随机数
		}
		holes[id].mole = Instantiate(moleObj, new Vector3(holes[id].holeX, holes[id].holeY, 0), Quaternion.identity);
		holes[id].mole.GetComponent<Mole>().Id = id;
		//2、在对应 ID 的洞口实例化地鼠对象。
		holes[id].isAppear = true; //2、把对应 ID 的洞口里边的isAppear 设置为 true，这样设置是为了判断是否该洞口有没有地鼠。

		// Debug.Log("MoleAppear");
	}

	private void CleanHoleState()
	{
		//2、清除洞口地鼠信息
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				if (holes[i * 3 + j].mole == null)
				{
					holes[i * 3 + j].isAppear = false;
				}
			}
		}
	}
}
